﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OnWork.DataCenter.Utils.Formula
{ 
    /// <summary>
    /// 公式方法
    /// </summary>
    public class ExpressionContext
    {
        /// <summary>
        /// 当前时间
        /// </summary>
        public string Now { get; set; } = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.ffffff");

        public ExpressionContext()
        {
            //var r = Now;
        }

        /// <summary>
        /// 当前设置
        /// </summary>
        public object? Self { get; set; }

        public IDictionary<string, object>? Parameters  { get; set; }


        public string Today()
        {
            return DateToStr(DateTime.Today);
        }

        public string DATEDELTA(string date, int deltadays)
        {
            DateTime dt = DateTime.Parse(date);
            dt = dt.AddDays(deltadays);
            return DateToStr(dt);
        }

        private string DateToStr(DateTime date)
        {
            return date.ToString("yyyy-MM-dd");
        }

        /// <summary>
        /// 时间戳转日期
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        public string TimeStampToDateStr(string timestampStr)
        {
            long timestamp = long.Parse(timestampStr);
            long begtime = timestamp * 10000;
            DateTime dt_1970 = new DateTime(1970, 1, 1, 0, 0, 0);
            long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
            long time_tricks = tricks_1970 + begtime;//日志日期刻度
            DateTime dt = new DateTime(time_tricks);//转化为DateTime
            dt = dt.AddHours(8);//转换成北京时间
            return dt.ToString("yyyy-MM-dd HH:mm:ss");
        }

        /// <summary>
        /// 返回字典类型
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string P(string key) {
            return Parameters[key].ToString();
        }
    }
}
